<template>
  <div class="login-container">
    <el-card>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >
        <div class="title-container">
          <h3 class="title">{{ $t('uselogs') }}</h3>
          <!-- 国际化 -->
          <Lang style="color: black" />
        </div>

        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>

        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
        >
          {{ $t('login.login') }}
        </el-button>

        <div class="tips">
          <span style="margin-right: 20px">username: root@admin.com</span>
          <span> password: 123456</span>
        </div>
        <div>
          <a @click.prevent="$router.push('/reg')">去注册</a>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import Shajs from 'jssha'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'root@admin.com',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern:
              /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
            message: '用户名必须是邮箱',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 15,
            message: '用户名要求3-15个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15位的非空字符',
            trigger: 'blur'
          }
        ]
      },
      loading: false,
      passwordType: 'password'
    }
  },
  methods: {
    // 密码是否可见
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      // 1. 生成 shajs 实例
      // eslint-disable-next-line no-undef
      const shajs = new Shajs('SHA-256', 'TEXT', { encoding: 'UTF8' })
      // 2. 要加密的密码
      shajs.update(this.loginForm.password)
      // 3. 获取
      const pwd = shajs.getHash('HEX')
      // console.log(pwd)
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            await this.$store.dispatch('user/loginAction', {
              ...this.loginForm,
              password: pwd
            })
            await this.$store.dispatch('user/getuserInfoAction')
            this.$message.success('登陆成功')
            // 优先跳至回跳地址，否则去首页
            this.$router.replace(this.$route.query.redirectUrl || '/')
          } catch (error) {
            console.log(error)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" >
* {
  padding: 0;
  margin: 0;
}

$bg: #fff;
$light_gray: black;
$cursor: black;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #ccc;
  height: 100vh;
  background: url(~@/assets/images/login.png) no-repeat 0 0/100vw 100vh;

  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      // padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(133, 133, 133, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$dark_gray: #889aa4;
$light_gray: #111010;

.login-container {
  min-height: 100%;
  width: 100%;
  overflow: hidden;
  .el-card {
    background-color: #f5f5f5;
    margin-right: 30%;
  }
  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #888484;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;
    display: flex;
    justify-content: space-between;
    margin: 40px 0;
    ::v-deep .svg-icon[data-v-c8a70580] {
      color: #625f5f !important;
    }
    .title {
      font-size: 26px;
      font-weight: 400;
      text-align-last: left;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
